package fi.sigmasolutions.maximo.testing;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import se.sigma.selenium.testing.SeleniumTest;

/**
 *
 * @author tupa1617
 */
public abstract class MaximoPage extends SeleniumTest {

    protected static final String NAVIGATION = "//div[@id='navigation']";
    protected static final String LOADING = "//div[@class='loading-animation']";
    protected static final String LOGIN_PAGE = "//div[@class='crmLogin']";
    protected static final String SIGNOUT_LINK = "//div[@class='sign-out']/a";
    protected static final String ADD_ACCOUNT = "//div[@class='actions']/div/a[contains(@title, 'New Account')]";

    public static final String PAGE_TITLE_HOME = "Home";
    public static final String PAGE_TITLE_CUSTOMERS = "Customers";
    public static final String PAGE_TITLE_ORDER_LIST = "Order list";
    public static final String PAGE_TITLE_CREATE_ACCOUNT = "CreateNewAccount";

    public MaximoPage(WebDriver driver) {
        super(driver);
        waitForPageToLoad(120);
    }

    public MaximoPage(WebDriver driver, String pageTitle) {
        super(driver);
        waitForPageToLoad(120);
        
        if (!driver.getTitle().contains(pageTitle)) {
            throw new IllegalStateException("Mismatch " + pageTitle + "/" + driver.getTitle() + " : " + driver.getCurrentUrl());
        }
    }

//    public CreateAccountPage createNewAccount() {
//        
//        WebElement link = driver.findElement(By.xpath(ADD_ACCOUNT));
//        link.click();
//        
//        return new CreateAccountPage(driver);
//        
//    }

    public MaximoPage navigateTo(String link) {
        
        // Check if we are seeing the navigation. If not, check if we are in the login page
        if (!isElementPresent(By.xpath(NAVIGATION))) {
            if (isElementPresent(By.xpath(LOGIN_PAGE))) {
                return new LoginPage(driver);
            } else {
                throw new IllegalStateException("This is not correct: " + driver.getCurrentUrl());
            }
        }

        //String searchPagePath = "//div[@id='navigation']/ul/li/ul/li/a[text()='" + link + "']";
        String mainNavigationPath = "//div[@id='navigation']/ul/li/a/span[text()='" + link + "']";
        WebElement navi = driver.findElement(By.xpath(mainNavigationPath));
        navi.click();
        
        switch (link) {
            case PAGE_TITLE_HOME:
                return new HomePage(driver);
//            case PAGE_TITLE_CUSTOMERS:
//                return new CustomersPage(driver);
            default:
                throw new IllegalStateException("This is not correct: " + driver.getCurrentUrl());
        }
    }

    public LoginPage signOut() {

        WebElement signOut = driver.findElement(By.xpath(SIGNOUT_LINK));
        signOut.click();

        return new LoginPage(driver);

    }
    
    protected final void waitForLoadingToFinish(int timeoutInSeconds) {
        try {
            waitForElementNotPresent(By.xpath(LOADING), timeoutInSeconds);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    protected final void waitForPageToLoad(int timeoutInSeconds) {
                
        try {
            if (driver instanceof JavascriptExecutor) {
                JavascriptExecutor jsDriver = (JavascriptExecutor) driver;

                for (int i = 0; i < timeoutInSeconds; i++) {
                    Object numberOfAjaxConnections = jsDriver.executeScript("return jQuery.active");
                    // return should be a number
                    if (numberOfAjaxConnections instanceof Long) {
                        Long n = (Long) numberOfAjaxConnections;
                        
                        if (n.longValue() == 0L) {
                            break;
                        }
                    }
                    Thread.sleep(750);
                }
            } else {
                log("Web driver: " + driver + " cannot execute javascript");
            }
        } catch (InterruptedException e) {
            System.out.println(e);
        }
    }
}